package com.njupt.Array;

public class BinarySearch {

    /**
     * 二分查找左闭右闭区间
     * @param nums
     * @param target
     * @return
     */
    public int search(int[] nums, int target) {
        int left  = 0;
        int right = nums.length-1;
        int middle = -1;
        while(left <= right){
//            middle = (left + right) / 2;
            middle = left + (right - left) / 2;
//            middle = left + (right - left) >> 1;
            if(target == nums[middle]){
                return middle;
            }else if(target < nums[middle]){
                right = middle -1;
            }else {
                left = middle + 1;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[]  nums = {-1,0,3,5,9,12};

        BinarySearch test = new BinarySearch();

        System.out.println(test.search(nums, 9));
    }
}
